<?php
/**
 *
 * @author    Lucian Daia < lucian.daia@live.com >
 * @copyright 2010, Lucian Daia
 * @package   KChunk
 */
class KChunk_WorkerPool
{
	/**
	 * Maximum number of workers to spawn
	 * @var int
	 */
	protected $_workerLimit = null;

	/**
	 * Array of workers (KChunk_HttpWorker)
	 *
	 * @var array
	 */
	protected $_workers = array();

	/**
	 * URL of the resource that will be fetched
	 * @var string
	 */
	protected $_resourceUrl = null;

	/**
	 * Offset from the begining of the resource
	 * @var int
	 */
	protected $_startOffset = null;

	/**
	 * Size of the chunk to fetch (in bytes)
	 * @var int
	 */
	protected $_chunkSize = null;

	/**
	 * Constructor
	 * @param string $resUrl The URL of the resource to fetch
	 * @param int    $startOffset
	 * @param int    $chunkSize
	 */
	public function  __construct($resUrl, $workerLimit)
	{
		$this->_resourceUrl = $resUrl;
		$this->_startOffset = $startOffset;
		$this->_chunkSize   = $chunkSize;
	}

	/**
	 * Builds the command to be executed by the child process
	 *
	 * @todo add the second parameter: CHUNK_FILE_PREFIX
	 */
	protected function _buildCommand()
	{
		return sprintf('php slave.php %s %s %d %d',
			$this->_resourceUrl, '', $this->_startOffset, $this->_chunkSize);
	}
}